package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class DaFuWengRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (dfwConfig != null) {
            ActivityInfo activityInfo = dfwConfig.getActivityInfo();
            long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
            List<Integer> cardIdList = new ArrayList<>();
            cardIdList.add(eItemId.DFW_GOLD_CARD.getValue());
            cardIdList.add(eItemId.DFW_YAO_YAN_CARD.getValue());
            cardIdList.add(eItemId.DFW_ZHU_FU_CARD.getValue());
            cardIdList.add(eItemId.DFW_ENERGY_CARD.getValue());
            cardIdList.add(eItemId.DFW_LU_ZHANG_CARD.getValue());
            cardIdList.add(eItemId.DFW_YONG_BING_CARD.getValue());
            cardIdList.add(eItemId.DFW_GOD_CARD.getValue());
            cardIdList.add(eItemId.DFW_HUO_WU_CARD.getValue());
            cardIdList.add(eItemId.DFW_ZHUI_BU_CARD.getValue());
            cardIdList.add(eItemId.DFW_RANDOM_CARD.getValue());
            for (Integer cardId : cardIdList) {
                List<String> paramList = new ArrayList<>();
                paramList.add(activityInfo.getActivityName());
                ActivityItemRecycleMgr.addRecycleItemData(cardId,activityType,2,recycleTime,paramList);
            }
            List<String> paramList = new ArrayList<>();
            paramList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.DFW_ENERGY_ITEM.getValue(),activityType,1,recycleTime,paramList);
        }

    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.DaFuWeng.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if (recycleType == 1) {
            recycleEnergyItem(userId, data);
        } else if (recycleType == 2) {
            recycleCard(userId, data);
        }
    }

    @Override
    public boolean isActivityRunning() {
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (dfwConfig != null) {
            return ActivityMgr.activityInTime(dfwConfig.getActivityInfo());
        }
        return false;
    }

    private void recycleEnergyItem(long userId, ActivityItemRecycleData data) {
        String language = UserMgr.getUserInfo(userId).getLanguage();
        int itemId = data.getItemId();
        List<String> paramList = data.getParamList();
        String activityName = paramList.get(0);
        String energyItemName = "";
        int energyItemCount = 0;
        String scoreItemName = "";
        GoodsInfo scoreItemInfo = GoodsMgr.getGoodsById(eItemId.DFW_GOLDEN.getValue());
        if (scoreItemInfo != null) {
            scoreItemName = ServerLanguageMgr.getContent(scoreItemInfo.getGoodsName(), language);
        }
        Property getProp = new Property();

        // 体力道具换积分
        GoodsInfo energyGoodsInfo = GoodsMgr.getGoodsById(itemId);
        UserBagItem energyUserBagItem = getUserBagItem(userId, itemId);
        if (energyGoodsInfo != null) {
            energyItemName = ServerLanguageMgr.getContent(energyGoodsInfo.getGoodsName(), language);
        }
        if (energyGoodsInfo != null && energyUserBagItem != null && energyUserBagItem.getNum() > 0) {
            energyItemCount = (int) energyUserBagItem.getNum();
            getProp.addProperty(eItemId.DFW_GOLDEN.getValue(), energyGoodsInfo.getParamList().get(1).multiply(BigInteger.valueOf(energyItemCount)));
        }
        if (removeItem(energyUserBagItem)) {
            if (energyItemCount > 0) {
                activityName = ServerLanguageMgr.getContent(activityName, language);
                String title = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_TITLE, language), activityName);
                String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_CONTENT, language),
                        activityName, energyItemName, energyItemCount, 0, scoreItemName);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(getProp), content, title);
            }
        }
    }

    private void recycleCard(long userId, ActivityItemRecycleData data) {
        int cardCount = 0;
        List<String> paramList = data.getParamList();
        String activityName = paramList.get(0);
        String language = UserMgr.getUserInfo(userId).getLanguage();
        String scoreItemName = "";
        GoodsInfo scoreItemInfo = GoodsMgr.getGoodsById(eItemId.DFW_GOLDEN.getValue());
        if (scoreItemInfo != null) {
            scoreItemName = ServerLanguageMgr.getContent(scoreItemInfo.getGoodsName(), language);
        }
        int itemId = data.getItemId();
        Property getProp = new Property();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if (goodsInfo != null && userBagItem != null && userBagItem.getNum() > 0) {
            cardCount += userBagItem.getNum();
            getProp.addProperty(eItemId.DFW_GOLDEN.getValue(), goodsInfo.getParamList().get(1).multiply(BigInteger.valueOf(userBagItem.getNum())));
        }
        if (removeItem(userBagItem)) {
            if (cardCount > 0) {
                GoodsInfo energyGoodsInfo = GoodsMgr.getGoodsById(eItemId.DFW_ENERGY_ITEM.getValue());
                String energyItemName = "";
                if (energyGoodsInfo != null) {
                    energyItemName =  ServerLanguageMgr.getContent(energyGoodsInfo.getGoodsName(), language);
                }
                activityName = ServerLanguageMgr.getContent(activityName, language);
                String title = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_TITLE, language), activityName);
                String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_CONTENT, language),
                        activityName, energyItemName, 0, cardCount, scoreItemName);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(getProp), content, title);
            }
        }
    }
}
